﻿using SmallJob.Model;
using SmallJob.Services.Code;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Code;
using SmallShop.Services.Interfaces;
using System;
using System.Web.Mvc;

namespace BackEnd.Controllers
{
    [Authorize]
    public class EmailController : Controller
    {
        private readonly IUserService _UserService;
        private readonly IEmailFormatService _EmailFormatService;

        public EmailController(IUserService UserService, IEmailFormatService EmailFormatService)
        {
            _UserService = UserService;
            _EmailFormatService = EmailFormatService;
        }

        public ActionResult Index()
        {
            var model = _EmailFormatService.All();
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new EmailFormat();
            return View("Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var model = _EmailFormatService.GetById(id);
            return View(model);
        }

        [ValidateInput(false)]
        public ActionResult Save()
        {
            var id = Convert.ToInt32(Request.Form["id"]);
            var title = Request.Form["title"];
            var des = Request.Form["des"];
            var subject = Request.Form["subject"];
            var body = Request.Form["body"];
            var model = new EmailFormat();
            if (id > 0)
            {
                model = _EmailFormatService.GetById(id);
            }

            model.Title = title;
            model.Description = des;
            model.SubjectMail = subject;
            model.BodyEmail = body;
            if (id > 0)
            {
                _EmailFormatService.Update(model);
            }
            else
            {
                _EmailFormatService.Add(model);
            }
            return View("Edit", model);
        }

        public ActionResult Delete(int id)
        {
            var model = _EmailFormatService.GetById(id);
            model.IsDeleted = true;
            _EmailFormatService.Update(model);
            return RedirectToAction("Index");
        }

        public ActionResult UnDel(int id)
        {
            var model = _EmailFormatService.GetById(id);
            model.IsDeleted = false;
            _EmailFormatService.Update(model);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult SendMail()
        {
            ViewData["formats"] = _EmailFormatService.All();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SaveSendMail()
        {
            string to = Request.Form["txtTo"];
            string from = Request.Form["txtFrom"];
            string subject = Request.Form["txtSubject"];
            string dictionary = Request.Form["dictionary"];
            string body = Request.Form["txtBody"];
            int format = Convert.ToInt32(Request.Form["selectformat"]);

            EmailFormat origin = new EmailFormat();
            if (format != 0)
            {
                origin = _EmailFormatService.GetById(format);
            }
            else
            {
                origin.BodyEmail = body;
            }
            EmailFactory factory = new EmailFactory(origin);
            factory.FillDataToDictionary(dictionary);
            body = factory.ReplaceBodyWithDictionary();
            ViewData["formats"] = _EmailFormatService.All();
            if (format != 0)
            {
                subject = origin.SubjectMail;
            }
            subject = factory.ReplaceWithDictionary(subject);
            UserUtility.ViecNhoSendMail(to, subject, body);
            return View("SendMail");
        }

        public ActionResult ReferencesDictionaryEmail()
        {
            return View();
        }
    }
}